const { ccclass, property } = cc._decorator;

@ccclass
export default class btCdControl extends cc.Component {

    @property(cc.Node)
    cdNode: cc.Node = null;

    @property()
    cdTime: number = 1;

    _cdSprite: cc.Sprite = null;
    _isCding: boolean = false;
    _cdTime: number = 0;

    onLoad() {
        this.cdNode.active = false;
        this._cdSprite = this.cdNode.getComponent(cc.Sprite);
        this.node.on("click", this.btclick, this);
    }
    init() {
        this._cdTime = 1 / this.cdTime;
    }

    btclick() {
        this._cdSprite.fillRange = 1;
        this._isCding = true;
        this.cdNode.active = true;
        this.node.getComponent(cc.Button).enabled = false;
    }
    setDisable() {
        this._cdSprite.fillRange = 1;
        this.cdNode.active = true;
        this._isCding = false;
        this.node.getComponent(cc.Button).enabled = false;
    }
    setEnable() {
        if (!this._isCding) {
            this.cdNode.active = false;
            this.node.getComponent(cc.Button).enabled = true;
        }
    }

    update(dt) {
        if (this._isCding) {
            let cd = this._cdSprite.fillRange -= this._cdTime * dt;
            if (cd <= 0) {
                this.cdNode.active = false;
                this._isCding = false;
                this.node.getComponent(cc.Button).enabled = true;
            }
        }
    }
}
